package com.fita.admin.web.controller.finance;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.TransactionRecordSearch;
import com.fita.admin.service.agent.AgentCompanyService;
import com.fita.admin.service.agent.AgentService;
import com.fita.admin.service.report.TransactionRecordService;
import com.fita.entity.agent.AgentCompany;
import com.fita.entity.wallet.TransactionRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/29
 */
@Controller
@RequestMapping(path = "/transaction-records")
@MenuSetting("menu-report")
public class TransactionRecordController {

    @Autowired
    private TransactionRecordService transactionRecordService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private AgentCompanyService agentCompanyService;

    /**
     *
     * @param page
     * @param search
     * @param request
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-transaction-record-list")
    public ModelAndView list(Page page,
                             TransactionRecordSearch search,
                             HttpServletRequest request){

        ModelAndView modelAndView = new ModelAndView("report/transaction-record/transaction-record-list");

        int count = transactionRecordService.count(search);

        if (count > 0){

            List<TransactionRecord> transactionRecords = transactionRecordService.search(search, page.with(count));

            modelAndView.addObject("transactionRecords", transactionRecords);

            modelAndView.addObject("queryString", WebUtils.extractQueryString(request,"page", "filter"));

            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    /**
     *
     * @return
     */
    @GetMapping(path = "/search")
    public ModelAndView search(){

        ModelAndView modelAndView = new ModelAndView("report/transaction-record/transaction-record-search");

        List<AgentCompany> agentCompanies = agentCompanyService.listAll(AgentCompany.class);

        modelAndView.addObject("agentCompanies", agentCompanies);

        return modelAndView;
    }

}
